#include "communicate.hpp"

#include "driver/uart.h"

// 定义 UART 接口
HardwareSerial UartSerial(1);  // 使用 UART1
bool CommunicateOn = false;

RcType_t rc_type = RC_ET08A;  // 遥控器类型
RC_ctrl_u RC_data =
    {  // 遥控器数据
        .RC_ctrl = {
            .ch0 = RC_CH_VALUE_OFFSET,
            .ch1 = RC_CH_VALUE_OFFSET,
            .ch2 = RC_CH_VALUE_OFFSET,
            .ch3 = RC_CH_VALUE_OFFSET,
            .s0 = 1,
            .s1 = 1,
            .mouse_x = 0,
            .mouse_y = 0,
            .mouse_z = 0,
            .mouse_press_l = 0,
            .mouse_press_r = 0,
            .key = 0,
            .ch4 = RC_CH_VALUE_OFFSET,
        }};
SBUS_ctrl_u SBUS_data =
    {  // SBUS 遥控器数据
        .unpack = {
            .start = 0x0F,
            .end = 0x00,
        }};

void setupUART(RcType_t type)
{
    rc_type = type;  // 设置遥控器类型
    // 初始化 UART1
    // 波特率 100000，8 数据位，偶校验位，1 停止位，RX=GPIO12, TX=GPIO13
    UartSerial.begin(100000, SERIAL_8E1, 12, 13);
    // 设置 TXD 引脚为反相
    uart_set_line_inverse(UART_NUM_1, UART_SIGNAL_TXD_INV);

    Serial.print("遥控器类型: ");
    Serial.println(type);

    Serial.println("UART 已初始化");
}

void sendData()
{
    switch (rc_type) {
        case RC_DT7: {             // DT7 遥控器
            if (!CommunicateOn) {  // 如果通信未开启，则不发送数据
                return;
            }
            UartSerial.write(RC_data.pack.buf, RC_FRAME_LENGTH);  // 发送数据到 UART1
        } break;

        case RC_ET08A: {  // ET08A 遥控器
            if (CommunicateOn) {
                SBUS_data.unpack.connect_flag = ET08A_RC_CONNECTED_FLAG;
            } else {
                SBUS_data.unpack.connect_flag = ET08A_RC_DISCONNECTED_FLAG;
            }

            UartSerial.write(SBUS_data.pack.buf, SBUS_RC_FRAME_LENGTH);  // 发送数据到 UART1
        } break;

        default:
            break;
    }
}

void resetRCData(void)
{
    switch (rc_type) {
        case RC_DT7: {
            RC_data.RC_ctrl.ch0 = RC_CH_VALUE_OFFSET;
            RC_data.RC_ctrl.ch1 = RC_CH_VALUE_OFFSET;
            RC_data.RC_ctrl.ch2 = RC_CH_VALUE_OFFSET;
            RC_data.RC_ctrl.ch3 = RC_CH_VALUE_OFFSET;
            RC_data.RC_ctrl.ch4 = RC_CH_VALUE_OFFSET;
            RC_data.RC_ctrl.s0 = RC_SW_DOWN;
            RC_data.RC_ctrl.s1 = RC_SW_DOWN;
            RC_data.RC_ctrl.mouse_x = 0;
            RC_data.RC_ctrl.mouse_y = 0;
            RC_data.RC_ctrl.mouse_z = 0;
            RC_data.RC_ctrl.mouse_press_l = 0;
            RC_data.RC_ctrl.mouse_press_r = 0;
            RC_data.RC_ctrl.key = 0;
        } break;

        case RC_ET08A: {
            SBUS_data.unpack.ch0 = ET08A_RC_CH_VALUE_OFFSET;
            SBUS_data.unpack.ch1 = ET08A_RC_CH_VALUE_OFFSET;
            SBUS_data.unpack.ch2 = ET08A_RC_CH_VALUE_OFFSET;
            SBUS_data.unpack.ch3 = ET08A_RC_CH_VALUE_OFFSET;
            SBUS_data.unpack.ch4 = ET08A_RC_CH_VALUE_OFFSET;
            SBUS_data.unpack.ch5 = ET08A_RC_CH_VALUE_OFFSET;
            SBUS_data.unpack.ch6 = ET08A_RC_CH_VALUE_OFFSET;
            SBUS_data.unpack.ch7 = ET08A_RC_CH_VALUE_OFFSET;
            SBUS_data.unpack.ch8 = ET08A_RC_CH_VALUE_OFFSET;
            SBUS_data.unpack.ch9 = ET08A_RC_CH_VALUE_OFFSET;
            SBUS_data.unpack.ch10 = ET08A_RC_CH_VALUE_OFFSET;
            SBUS_data.unpack.ch11 = ET08A_RC_CH_VALUE_OFFSET;
            SBUS_data.unpack.ch12 = ET08A_RC_CH_VALUE_OFFSET;
            SBUS_data.unpack.ch13 = ET08A_RC_CH_VALUE_OFFSET;
            SBUS_data.unpack.ch14 = ET08A_RC_CH_VALUE_OFFSET;
            SBUS_data.unpack.ch15 = ET08A_RC_CH_VALUE_OFFSET;
        } break;

        default:
            break;
    }
}

void updateRCData(int channel, int16_t value)
{
    switch (rc_type) {
        case RC_DT7: {
            switch (channel) {
                case 0: {  // ch0
                    RC_data.RC_ctrl.ch0 = RC_CH_VALUE_OFFSET + value;
                    Serial.println("ch0: " + String(RC_data.RC_ctrl.ch0));
                } break;

                case 1: {  // ch1
                    RC_data.RC_ctrl.ch1 = RC_CH_VALUE_OFFSET + value;
                    Serial.println("ch1: " + String(RC_data.RC_ctrl.ch1));
                } break;

                case 2: {  // ch2
                    RC_data.RC_ctrl.ch2 = RC_CH_VALUE_OFFSET + value;
                    Serial.println("ch2: " + String(RC_data.RC_ctrl.ch2));
                } break;

                case 3: {  // ch3
                    RC_data.RC_ctrl.ch3 = RC_CH_VALUE_OFFSET + value;
                    Serial.println("ch3: " + String(RC_data.RC_ctrl.ch3));
                } break;

                case 4: {  // ch4
                    RC_data.RC_ctrl.ch4 = RC_CH_VALUE_OFFSET + value;
                    Serial.println("ch4: " + String(RC_data.RC_ctrl.ch4));
                } break;

                case 5: {  // s0
                    RC_data.RC_ctrl.s0 = value;
                    Serial.println("s0: " + String(RC_data.RC_ctrl.s0));
                } break;

                case 6: {  // s1
                    RC_data.RC_ctrl.s1 = value;
                    Serial.println("s1: " + String(RC_data.RC_ctrl.s1));
                } break;

                default:
                    break;
            }
        } break;

        case RC_ET08A: {
            switch (channel) {
                case 0: {  // dt7 ch0
                    SBUS_data.unpack.ch0 = ET08A_RC_CH_VALUE_OFFSET + value;
                    Serial.println("ch0: " + String(SBUS_data.unpack.ch0));
                } break;

                case 1: {  // dt7 ch1
                    SBUS_data.unpack.ch1 = ET08A_RC_CH_VALUE_OFFSET + value;
                    Serial.println("ch1: " + String(SBUS_data.unpack.ch1));
                } break;

                case 2: {  // dt7 ch2
                    SBUS_data.unpack.ch3 = ET08A_RC_CH_VALUE_OFFSET + value;
                    Serial.println("ch3: " + String(SBUS_data.unpack.ch3));
                } break;

                case 3: {  // dt7 ch3
                    SBUS_data.unpack.ch2 = ET08A_RC_CH_VALUE_OFFSET + value;
                    Serial.println("ch2: " + String(SBUS_data.unpack.ch2));
                } break;

                case 5: {  // dt7 s0
                    SBUS_data.unpack.ch5 = value;
                    Serial.println("ch5: " + String(SBUS_data.unpack.ch5));
                } break;

                case 6: {  // dt7 s1
                    SBUS_data.unpack.ch4 = value;
                    Serial.println("ch4: " + String(SBUS_data.unpack.ch4));
                } break;

                case 4:  // dt7 ch4
                default:
                    break;
            }
        } break;
        default:
            break;
    }
}

void communicateOn(void)
{
    resetRCData();
    CommunicateOn = true;
    Serial.println("Communicate On");
}

void communicateOff(void)
{
    CommunicateOn = false;
    Serial.println("Communicate Off");
}
// END =====================================================
